package com.yuanguangsheng.disk.spring.util;

import com.obs.services.ObsClient;
import com.obs.services.model.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public final class ObsUtil {

    private static  final  String endpoint="obs.cn-north-4.myhuaweicloud.com";
    private static  final  String  ak = "HPUASTRAEKCOYPO4OQOO";
    private static  final  String  sk = "GQXNQGzA3SBL6s0JKvq9EIuCDnmHbqYhVGTQwu61";
    private static  final  String  bucketName = "java-test1";
    public static void main(String[] args) {
        ObsClient obsClient = new ObsClient(ak,sk,endpoint);
        //obs列表
//        ObjectListing list = obsClient.listObjects(bucketName);
//        List<ObsObject> listObjects = list.getObjects();
//        for(ObsObject obsObject:listObjects){
//            System.out.println("obsKey:" + obsObject.getObjectKey());
//            System.out.println("obsSize:"  + obsObject.getMetadata().getContentLength());
//        }
        //文件下载
//        ObsObject obsObject = obsClient.getObject(bucketName, "2.mp4");
//        InputStream is = obsObject.getObjectContent();
//        byte[] block = new byte[1024];
//        Integer length = 0;
//        try(  FileOutputStream fileOutputStream =  new FileOutputStream("D:\\logfile\\2.mp4 ");
//        ){
//            while ((length =  is.read(block)) > 0 ){
//                fileOutputStream.write(block,0,length);
//                fileOutputStream.flush();
//            }
//        }catch (IOException e1){
//            e1.printStackTrace();
//        }
//
        //上传文件
//        try (FileInputStream inputStream = new FileInputStream("D:\\logfile\\bb1.jpg");){
//            obsClient.putObject(bucketName,"bb1.jpg",inputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        //删除文件
//        String objKey = "bb1.jpg";
//        if(obsClient.doesObjectExist(bucketName,objKey)){
//            obsClient.deleteObject(bucketName,objKey);
//        }

        //文件匿名上传
//        try (FileInputStream inputStream = new FileInputStream("D:\\logfile\\bb1.jpg");){
//            obsClient.putObject(bucketName,"bb1.jpg",inputStream);
//            AccessControlList acl = new AccessControlList();
//
//            AccessControlList obsObject = obsClient.getBucketAcl(bucketName);
//            Owner owner = obsObject.getOwner();
//            acl.setOwner(owner);
//            acl.grantPermission(new CanonicalGrantee(owner.getId()), Permission.PERMISSION_FULL_CONTROL);
//            acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
//            acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_WRITE);
//            acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ_ACP);
//            acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_WRITE_ACP);
//            obsClient.setObjectAcl(bucketName, "bb1.jpg", acl);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        //文件的分享
//        String obsKey = "bb1.jpg";
//        TemporarySignatureRequest request = new TemporarySignatureRequest();
//        request.setBucketName(bucketName);
//        request.setObjectKey(obsKey);
//        request.setRequestDate(new Date());
//        request.setExpires(60*60);
//        TemporarySignatureResponse signature = obsClient.createTemporarySignature(request);
//        String url = signature.getSignedUrl();
//        System.out.println(url);



    }


}
